<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>创建一对完全一样的网络套接字连接流</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.stream-socket-get-name.html">« stream_socket_get_name</a></li>
      <li style="float: right;"><a href="function.stream-socket-recvfrom.html">stream_socket_recvfrom »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.stream.html">Stream 函数</a></li>
    <li>创建一对完全一样的网络套接字连接流</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.stream-socket-pair" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">stream_socket_pair</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.1.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">stream_socket_pair</span> &mdash; <span class="dc-title">
   创建一对完全一样的网络套接字连接流
  </span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.stream-socket-pair-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>stream_socket_pair</strong></span>(<span class="methodparam"><span class="type">int</span> <code class="parameter">$domain</code></span>, <span class="methodparam"><span class="type">int</span> <code class="parameter">$type</code></span>, <span class="methodparam"><span class="type">int</span> <code class="parameter">$protocol</code></span>): <span class="type"><span class="type">array</span>|<span class="type"><span class="type false">false</span></span></span></div>

  <p class="para rdfs-comment">
   <span class="function"><strong>stream_socket_pair()</strong></span> 创建一对完全一样的网络套接字连接，这个函数通常会被用在进程间通信（Inter-Process Communication）。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.stream-socket-pair-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">domain</code></dt>

     <dd>

      <p class="para">
       使用的协议族： <strong><code>STREAM_PF_INET</code></strong>,
       <strong><code>STREAM_PF_INET6</code></strong> or
       <strong><code>STREAM_PF_UNIX</code></strong>
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">type</code></dt>

     <dd>

      <p class="para">
       通信类型:
       <strong><code>STREAM_SOCK_DGRAM</code></strong>、
       <strong><code>STREAM_SOCK_RAW</code></strong>、
       <strong><code>STREAM_SOCK_RDM</code></strong>、
       <strong><code>STREAM_SOCK_SEQPACKET</code></strong>、
       <strong><code>STREAM_SOCK_STREAM</code></strong>
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">protocol</code></dt>

     <dd>

      <p class="para">
       使用的传输协议: <strong><code>STREAM_IPPROTO_ICMP</code></strong>、
       <strong><code>STREAM_IPPROTO_IP</code></strong>、
       <strong><code>STREAM_IPPROTO_RAW</code></strong>、
       <strong><code>STREAM_IPPROTO_TCP</code></strong>、
       <strong><code>STREAM_IPPROTO_UDP</code></strong>       
      </p>
     </dd>

    
   </dl>

  </p>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
   <span class="simpara">
    关于每个常量的更多细节，请查阅 <a href="stream.constants.html" class="link">Stream 常量列表</a>。
   </span>
  </p></blockquote>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.stream-socket-pair-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   成功时将返回一个包含了两个 socket 资源的 <span class="type">array</span>，错误时返回 <strong><code>false</code></strong>。
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-function.stream-socket-pair-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-3785">
    <p><strong>示例 #1 <span class="function"><strong>stream_socket_pair()</strong></span> 例子</strong></p>
    <div class="example-contents"><p>
     这个例子展示了
     <span class="function"><strong>stream_socket_pair()</strong></span> 进程间通信的基本用法。
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />$sockets&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">stream_socket_pair</span><span style="color: #007700">(</span><span style="color: #0000BB">STREAM_PF_UNIX</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">STREAM_SOCK_STREAM</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">STREAM_IPPROTO_IP</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$pid&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">pcntl_fork</span><span style="color: #007700">();<br /><br />if&nbsp;(</span><span style="color: #0000BB">$pid&nbsp;</span><span style="color: #007700">==&nbsp;-</span><span style="color: #0000BB">1</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;die(</span><span style="color: #DD0000">'could&nbsp;not&nbsp;fork'</span><span style="color: #007700">);<br /><br />}&nbsp;else&nbsp;if&nbsp;(</span><span style="color: #0000BB">$pid</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;父进程&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$sockets</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$sockets</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">],&nbsp;</span><span style="color: #DD0000">"child&nbsp;PID:&nbsp;</span><span style="color: #0000BB">$pid</span><span style="color: #DD0000">\n"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">fgets</span><span style="color: #007700">(</span><span style="color: #0000BB">$sockets</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$sockets</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]);<br /><br />}&nbsp;else&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #FF8000">/*&nbsp;子进程&nbsp;*/<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$sockets</span><span style="color: #007700">[</span><span style="color: #0000BB">1</span><span style="color: #007700">]);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fwrite</span><span style="color: #007700">(</span><span style="color: #0000BB">$sockets</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">],&nbsp;</span><span style="color: #DD0000">"message&nbsp;from&nbsp;child\n"</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;echo&nbsp;</span><span style="color: #0000BB">fgets</span><span style="color: #007700">(</span><span style="color: #0000BB">$sockets</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]);<br /><br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">fclose</span><span style="color: #007700">(</span><span style="color: #0000BB">$sockets</span><span style="color: #007700">[</span><span style="color: #0000BB">0</span><span style="color: #007700">]);<br />}<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
child PID: 1378
message from child
</pre></div>
    </div>
   </div>
  </p>
 </div>


</div></div></div></body></html>